shell script|November 24, 2017|1 min read

Shell script to extract single file from a bunch of jar files

TL;DR

Use a shell script with unzip command and a for loop to extract a specific file from each jar in a directory, organizing output into per-jar folders.

Shell script to extract single file from a bunch of jar files

Problem Statement

I have a bunch of jar files lying in a folder. Lets name it /Users/labuser/jars

Each jar file has a manifest file in META_INF/MANIFEST.MF
I have to read each MANIFEST.MF file and get some information from it. So, instead of extracting whole jar, one by one is a tedius thing.

What I want

I want to extract that file in a folder whose name can be as of jar file.

Unzip command to extract a jar file

Following command can be used to unzip a complete jar file:

unzip <path of jar file>

If you want to unzip files to a particular folder

unzip <path of jar file> -d <path of target folder>

Note: if the target folder does not exists, it will be created

If you want to unzip only particular files

unzip <path of jar file> <path of file inside jar file>

If you want to unzip only particular files in a particular folder

unzip <path of jar file> <path of file inside jar file> -d <target folder>

Conclusion

That is all. Combine these, and you are done.

Shell script I used to solve this

for myfile in `ls /Users/labuser/jars` ; do unzip ../$myfile META-INF/MANIFEST.MF -d $myfile; done

Related Posts

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Jira Rest APIs common usages

Jira Rest APIs common usages

This article shows some of common usages of JIRA rest apis. Note: This article…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…